home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / knowhow4 / image.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-10  |  4.6 KB  |  164 lines

  1. #include "image.h"
  2. #include "ic_part.h"
  3.  
  4. #include <alloc.h>
  5. #include <mem.h>
  6.  
  7. #include <stdio.h>
  8.  
  9. inline unsigned char hi(unsigned char pix1, int pos)
  10.     { return ((unsigned char)(pix1 >> (8 - pos % 8))) << (8 - pos % 8); }
  11. inline unsigned char lo(unsigned char pix, int pos)
  12.     { return ((unsigned char)(pix << (pos % 8 + 1))) >> (pos % 8 + 1); }
  13. /////////////////////////////
  14. void image_put_pixel(imageP image, loc pos, int col, int bitpx, int nplanes)
  15.     {
  16.     int line_size = ((image->xmax + 1) * bitpx + 7) >> 3;
  17.     int start = line_size * pos.Y * nplanes;
  18.     int work_byte = pos.X * bitpx >> 3;
  19.  
  20.     unsigned char p, pix;
  21.     register int plane;
  22.     for(plane = 0; plane < nplanes; plane++, work_byte += line_size)
  23.     {
  24.     pix = p = image->data[start + work_byte];
  25.  
  26.     switch(bitpx)
  27.         {
  28.         case 1:
  29.         image->data[start + work_byte]
  30.             = (unsigned char)(hi(pix, pos.X) | lo(p, pos.X)
  31.                  | (((col & (1 << (nplanes - plane - 1))) >> (nplanes - plane - 1))
  32.                    << (7 - pos.X % 8)));
  33.         break;
  34.         case 2:
  35.         image->data[start + work_byte]
  36.             = (unsigned char)(hi(pix, pos.X * 2)
  37.                     | lo(p, pos.X * 2 + 1)
  38.                  | ((col & 3) << (6 - (pos.X * 2) % 8)));
  39.         break;
  40.         case 8:
  41.         image->data[start + work_byte]
  42.             = (unsigned char)col;
  43.         break;
  44.         }
  45.     }
  46.     }
  47. /////////////////////////////
  48. int image_get_pixel(imageP image, loc pos, int bitpx, int nplanes)
  49.     {
  50.     int line_size = ((image->xmax + 1) * bitpx + 7) >> 3;
  51.     int start = line_size * pos.Y * nplanes;
  52.     int work_byte = pos.X * bitpx >> 3;
  53.  
  54.     unsigned char pix, result = 0;
  55.     register int plane;
  56.     int step = (7 - pos.X % 8);
  57.     for(plane = 0; plane < nplanes; plane++, work_byte += line_size)
  58.     {
  59.     switch(bitpx)
  60.         {
  61.         case 1:
  62.         pix = image->data[start + work_byte];
  63.         result = result
  64.              | ((pix >> step & 1) << (nplanes - plane - 1));
  65.         break;
  66.         case 2:
  67.         pix = image->data[start + work_byte];
  68.         result = ((pix >> (6 - (pos.X * 2) % 8)) & 3);
  69.         break;
  70.         case 8:
  71.         result = image->data[start + work_byte];
  72.         break;
  73.         }
  74.     }
  75.     return result;
  76.     }
  77. /////////////////////////////
  78. void image_screen(imageP image, rect src, loc dest,   // src - in image, dest - on screen
  79.          int bitpx, int nplanes,
  80.          loc comp_s,                   // divx, divy, multx, multy
  81.          loc comp_d, int flag)         // rects are the outlines
  82.     {                                          // image show with deformation
  83.     if(src.height() > image->ymax + 1)
  84.         src.corner.Y = src.origin.Y + image->ymax + 1;
  85.  
  86.     if(comp_s.X != comp_d.X || comp_s.Y != comp_d.Y || flag)
  87.     {
  88.     long is, js;                         // source x and y
  89.     int id, jd;                          // destination (screen) x and y
  90.     int step_y = ((long)comp_s.Y << 10) / comp_d.Y;  // step in src
  91.     int step_x = ((long)comp_s.X << 10) / comp_d.X;
  92.     for(js = (long)src.origin.Y << 10, jd = dest.Y;
  93.         js < (long)src.corner.Y << 10; js += step_y, jd++)
  94.         for(is = (long)src.origin.X << 10, id = dest.X;
  95.         is < (long)src.corner.X << 10; is += step_x, id++)
  96.         {
  97.         int pix = image_get_pixel(image, loc(is >> 10, js >> 10),
  98.             bitpx, nplanes);
  99.                putpixel(id, jd, pix);
  100.         }
  101.     }
  102.     else
  103.     {
  104.     loc reserv = loc(image->xmax, imageP(image)->ymax);
  105.         cut(image, src);
  106.     putimage(dest.X, dest.Y, image, COPY_PUT);
  107.     image->xmax = reserv.X;
  108.     image->ymax = reserv.Y;
  109.     }
  110.     }
  111. /////////////////////////////
  112. void cpy(char* de, char* sr, int l)
  113.     {
  114.     for(int i = 0; i < l; i++)
  115.     de[i] = sr[i];
  116.     }
  117.  
  118. void screen_image(imageP image, rect src, loc dest) // no deformation
  119.     {
  120.     int src_bytes_plane = (src.width() + 7) >> 3;
  121.     int dst_bytes_plane = (image->xmax + 8) >> 3;
  122.     char work[400];
  123.     int dst = dst_bytes_plane << 2;
  124.     int vshift;
  125.  
  126.     imageP im = (imageP)work;
  127.     int hshift = dest.X >> 3;
  128.  
  129.     for(int y = 0; y < src.height(); y++)
  130.     {
  131.     getimage(src.origin.X, src.origin.Y + y, src.corner.X,
  132.          src.origin.Y + y, im);
  133.     vshift = dest.Y + y;
  134.     for(int d = 0; d < 4; d++)
  135.         {
  136.         cpy(image->data + vshift * dst
  137.            + dst_bytes_plane * d + hshift,
  138.            im->data + src_bytes_plane * d, src_bytes_plane);
  139.         }
  140.     }
  141.     }
  142. /////////////////////////////
  143.  
  144. /*
  145. void main()
  146.     {
  147.     int gdriver = VGA, gmode = VGAHI;
  148.     initgraph(&gdriver, &gmode, "..\\BGI");
  149.  
  150.     bar(10, 10, 100, 100);
  151.     setcolor(3);
  152.     circle(50, 50, 40);
  153.     line(0, 0, 100, 100);
  154.  
  155.     imageP image = (imageP)malloc(imagesize(0, 0, 40, 40));
  156.     getimage(10, 10, 49, 49, image);
  157.  
  158.     image_screen(image, rect(0, 0, 35, 35), loc(100, 100),
  159.          1, 4, loc(2, 2), loc(10, 10));
  160.  
  161.     delete image;
  162.     closegraph();
  163.     }
  164. */